<template>
    <div>
        <section>
            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <el-form-item>
                        <el-input v-model="filters.keyword" placeholder="关键字搜索" clearable></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" v-on:click="search">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="dictionaryTypes" highlight-current-row v-loading="listLoading"
                      @selection-change="selsChange" @row-dblclick="getShow" style="width: 100%; ">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column type="index" width="100">
                </el-table-column>
                <el-table-column prop="name" label="数据字典名" width="300" sortable>
                </el-table-column>
                <el-table-column prop="sn" label="编号" width="300" sortable>
                </el-table-column>
                <!--<el-table-column prop="dictionaryDetail.name" label="明细名" width="100" sortable>
                </el-table-column>-->
                <!--<el-table-column prop="parent.name" label="管理店铺" width="120" sortable>
                </el-table-column>-->
                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
                <el-pagination layout="prev, pager, next"
                               @current-change="handleCurrentChange"
                               :page-size="pageSize"
                               :current-page="currentPage"
                               :total="total"
                               style="float:right;">
                </el-pagination>
            </el-col>

            <!--编辑界面
              dictionaryTypeFormVisible 模态框是否显示
              dictionaryTypeFormRules  校验规则
              ref="dictionaryTypeForm"  给当前表单取别名  ——> refs
            -->
            <el-dialog title="明细新增或编辑" :visible.sync="dictionaryDetailFormVisible" :close-on-click-modal="false">
                <el-form :model="dictionaryDetailForm" label-width="80px"
                         :rules="dictionaryDetailFormRules"
                         ref="dictionaryDetailForm">
                    <el-form-item label="字典名" prop="name">
                        <el-input v-model="dictionaryDetailForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click.native="dictionaryDetailFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="dictionaryDetailSubmit" :loading="dictionaryDetailLoading">提交
                    </el-button>
                </div>
            </el-dialog>

            <el-dialog title="新增或编辑" :visible.sync="dictionaryTypeFormVisible" :close-on-click-modal="false">
                <el-form :model="dictionaryTypeForm" label-width="80px"
                         :rules="dictionaryTypeFormRules"
                         ref="dictionaryTypeForm">
                    <el-form-item label="字典名" prop="name">
                        <el-input v-model="dictionaryTypeForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="编号" prop="sn">
                        <el-input v-model="dictionaryTypeForm.sn" auto-complete="off"></el-input>
                    </el-form-item>
                    <!--<el-form-item label="管理部门">
                        &lt;!&ndash;<el-input v-model="dictionaryTypeForm.dictionaryDetail"></el-input>&ndash;&gt;
                        &lt;!&ndash;
                          :value="item"  可以传递一个对象过去
                           value-key="id"  通过id去找对象 然后赋值
                        &ndash;&gt;
                        <el-select v-model="dictionaryTypeForm.dictionaryDetail" value-key="id" placeholder="请选择"
                                   clearable>
                            <el-option
                                    v-for="item in dictionaryDetail"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item">
                                <span style="float: left">{{ item.name }}</span>
                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.types_id }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>-->
                    <!--<el-form-item label="父部门">
                        &lt;!&ndash;<el-input v-model="dictionaryTypeForm.parent"></el-input>&ndash;&gt;
                        <el-cascader v-model="dictionaryTypeForm.parent"
                                     :options="deptTree"
                                     :props="{
                                   checkStrictly: true,
                                   label:'name',
                                   value:'id'
                                 }"
                                     clearable></el-cascader>
                    </el-form-item>-->
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click.native="dictionaryTypeFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="dictionaryTypeSubmit" :loading="dictionaryTypeLoading">提交
                    </el-button>
                </div>
            </el-dialog>
        </section>
        <el-drawer
                title="数据字典明细"
                :visible.sync="table"
                direction="rtl"
                size="50%">
            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filtersTails">
                    <el-form-item>
                        <el-input v-model="filtersTails.keywordtails" placeholder="关键字搜索" clearable></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" v-on:click="searchTails">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAddTails">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoadingTails"
                      @selection-change="selsChangeTails" style="width: 100%; ">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column type="index" width="100">
                </el-table-column>
                <el-table-column prop="name" label="数据字典明细" width="300" sortable>
                </el-table-column>
                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button size="small" @click="handleEditTails(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDelTails(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-button type="danger" @click="batchRemoveTails" :disabled="this.sels.length===0">批量删除</el-button>
                <el-pagination layout="prev, pager, next"
                               @current-change="handleCurrentChangeTails"
                               :page-size="tailpageSize"
                               :current-page="tailcurrenPage"
                               :total="tailtotal"
                               style="float:right;">
                </el-pagination>
            </el-col>
        </el-drawer>

    </div>
</template>

<script>
    /*import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, dictionaryTypeEmployee, addEmployee } from '../../api/api';
*/
    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                filtersTails: {
                    keywordtails: ''
                },
                table:false,
                dictionaryTypes: [],
                dictionaryDetails:[],
                total: 0,
                tailtotal:0,
                currentPage: 1,
                tailcurrenPage:1,
                pageSize: 6,
                tailpageSize: 6,
                listLoading: false,
                listLoadingTails: false,
                sels: [],//列表选中行

                dictionaryTypeFormVisible: false,//编辑界面是否显示
                dictionaryTypeLoading: false,//提交按钮忙等
                dictionaryTypeFormRules: {//表单校验规则
                    name: [
                        {required: true, message: '请输入数据字典名', trigger: 'blur'}
                    ]
                },
                dictionaryDetailFormVisible: false,//编辑界面是否显示
                dictionaryDetailLoading: false,//提交按钮忙等
                dictionaryDetailFormRules: {//表单校验规则
                    name: [
                        {required: true, message: '请输入数据明细名', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                dictionaryTypeForm: {//表单模型数据
                    id: null,
                    name: "",
                    sn: "",
                    types_id:null,
                    dictionaryDetail: null
                },
                //编辑明细数据
                dictionaryDetailForm: {//表单模型数据
                    id: null,
                    name: "",
                    types_id:null,
                    dictionaryDetail: null
                },
                detailId:null,
                // 管理部门
                dictionaryDetail: [],
                /*// 部门级联树
                deptTree:[]*/
            }
        },
        methods: {
            getShow:function (row) {
                this.table =true;
                this.detailId = row.id;
                this.getDictionaryDetails();
            },
            search() {//只要是高级查询按钮一点击
                this.currentPage = 1;//定位到第一页
                this.getDictionaryTypes();
            },
            searchTails() {//只要是高级查询按钮一点击
                this.tailcurrenPage = 1;//定位到第一页
                this.getDictionaryDetails();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDictionaryTypes();
            },
            handleCurrentChangeTails(val) {
                this.tailcurrenPage = val;
                this.getDictionaryDetails();
            },

            //获取数据字典列表
            getDictionaryTypes() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/dictionaryType", para)
                    .then(result => {
                        result = result.data;// pageList
                        this.dictionaryTypes = result.rows;
                        this.total = result.total;
                        this.listLoading = false;

                    })
                    .catch(result => {
                        alert("系统错误！");
                    })
            },//获取数据字典明细列表
            getDictionaryDetails() {
                let para = {
                    currentPage: this.tailcurrenPage,
                    pageSize: this.tailpageSize,
                    keyword: this.filtersTails.keywordtails,
                    id:this.detailId
                };
                this.listLoadingTails = true;//开启忙等框
                this.$http.post("/dictionaryDetail", para)
                    .then(result => {
                        result = result.data;// pageList
                        this.dictionaryDetails = result.rows;
                        this.tailtotal = result.total;
                        this.listLoadingTails = false;

                    })
                    .catch(result => {
                        alert("系统错误！");
                    })
            },
            //删除数据字典
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.delete("/dictionaryType/" + row.id)
                        .then(result => {
                            this.listLoading = false;//关闭忙等框
                            //???  该完成什么事情
                            result = result.data;//AjaxResult {success:true,message:"操作成功"}
                            //AjaxResult {success:false,message:"操作失败"}
                            if (result.success) {
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                if (this.total % this.pageSize == 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getDictionaryTypes();
                            } else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        })
                }).catch(() => {

                });
            },

            //删除数据明细
            handleDelTails: function (index, row) {
                alert(row.types_id);
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoadingTails = true;//开启忙等框
                    //NProgress.start();
                    this.$http.delete("/dictionaryDetail/" + row.id)
                        .then(result => {
                            this.listLoadingTails = false;//关闭忙等框
                            //???  该完成什么事情
                            result = result.data;//AjaxResult {success:true,message:"操作成功"}
                            //AjaxResult {success:false,message:"操作失败"}
                            if (result.success) {
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                if (this.total % this.pageSize == 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getDictionaryDetails();
                            } else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.dictionaryTypeForm = Object.assign({}, row);//拷贝对象（回显）
                /*this.getDeptTree();*/
                /*let dirPath = row.dirPath;
                // 需要把字符串转换为数组
                if(dirPath){
                    let tmp = [];
                    let dirPaths =  dirPath.trim().split("/"); //['','2','5','11']
                    for(let i=1;i<dirPaths.length-1;i++){
                        tmp[i-1] = parseInt(dirPaths[i]);
                    }
                    this.dictionaryTypeForm.parent = tmp; //根据dirpath构造数组
                }*/
                this.dictionaryTypeFormVisible = true;//显示模态框
                /*重置表单校验*/
                this.$refs['dictionaryTypeForm'].resetFields();
            },
            //显示明细编辑界面
            handleEditTails: function (index, row) {
                this.dictionaryDetailForm = Object.assign({}, row);//拷贝对象（回显）
                this.dictionaryDetailFormVisible = true;//显示模态框
                /*重置表单校验*/
                this.$refs['dictionaryDetailForm'].resetFields();
            },
            //显示新增界面
            handleAdd: function () {
                this.dictionaryTypeFormVisible = true;//显示模态框
                this.dictionaryTypeForm = {//清空历史数据，不要ID
                    name: "",
                    sn: "",
                    dictionaryDetail: null
                    /*dictionaryDetail_id:null,
                    parent_id:0*/
                };
                /*this.getDeptTree();*/
                /*重置表单校验*/
                /*this.$refs['dictionaryTypeForm'].resetFields();*/
            },
            //显示明细新增界面
            handleAddTails: function () {
                this.dictionaryDetailFormVisible = true;//显示模态框
                this.dictionaryDetailForm = {//清空历史数据，不要ID
                    name: "",
                    types_id :this.detailId,
                    dictionaryDetail: null
                    /*dictionaryDetail_id:null,
                    parent_id:0*/
                };
                /*this.getDeptTree();*/
                /*重置表单校验*/
                /*this.$refs['dictionaryTypeForm'].resetFields();*/
            },
            //编辑提交按钮
            dictionaryTypeSubmit: function () {
                /*拿到当前vue里面的所有ref，从里面找到 dictionaryTypeForm*/
                this.$refs.dictionaryTypeForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '温馨提示', {})
                            .then(() => {
                                this.dictionaryTypeLoading = true;
                                //NProgress.start();
                                //准备表单数据提交给后台
                                let para = Object.assign({}, this.dictionaryTypeForm);

                                this.$http.put("/dictionaryType", para)
                                    .then(result => {
                                        //关闭忙等框
                                        this.dictionaryTypeLoading = false;
                                        result = result.data;
                                        if (result.success) {
                                            //NProgress.done();
                                            this.$message({
                                                message: '提交成功',
                                                type: 'success'
                                            });
                                            /*重置表单校验*/
                                            /*this.$refs['dictionaryTypeForm'].resetFields();*/
                                            this.dictionaryTypeFormVisible = false;
                                            /*刷新页面*/
                                            this.currentPage = 1;//定位到第一页
                                            this.getDictionaryTypes();
                                        } else {
                                            this.$message({
                                                message: result.message,
                                                type: 'error'
                                            });
                                        }
                                    });
                            }).catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        });
                    }
                });
            },
            //编辑提交按钮
            dictionaryDetailSubmit: function () {
                /*拿到当前vue里面的所有ref，从里面找到 dictionaryTypeForm*/
                this.$refs.dictionaryDetailForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '温馨提示', {})
                            .then(() => {
                                this.dictionaryDetailLoading = true;
                                //NProgress.start();
                                //准备表单数据提交给后台
                                let para = Object.assign({}, this.dictionaryDetailForm);

                                this.$http.put("/dictionaryDetail", para)
                                    .then(result => {
                                        //关闭忙等框
                                        this.dictionaryDetailLoading = false;
                                        result = result.data;
                                        if (result.success) {
                                            //NProgress.done();
                                            this.$message({
                                                message: '提交成功',
                                                type: 'success'
                                            });
                                            /*重置表单校验*/
                                            /*this.$refs['dictionaryTypeForm'].resetFields();*/
                                            this.dictionaryDetailFormVisible = false;
                                            /*刷新页面*/
                                            this.currentPage = 1;//定位到第一页
                                            this.getDictionaryDetails();
                                        } else {
                                            this.$message({
                                                message: result.message,
                                                type: 'error'
                                            });
                                        }
                                    });
                            }).catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        });
                    }
                });
            },

            selsChange: function (sels) {
                /*现目前已经选中的所有行的数据*/
                this.sels = sels;
            },
            selsChangeTails: function (sels) {
                /*现目前已经选中的所有行的数据*/
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);//[1,5]
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.patch("/dictionaryType", ids)
                        .then(result => {
                            this.listLoading = false;//关闭忙等框
                            //???  该完成什么事情
                            result = result.data;//AjaxResult {success:true,message:"操作成功"}
                            //AjaxResult {success:false,message:"操作失败"}
                            if (result.success) {
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                if (this.total % this.pageSize == 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getDictionaryTypes();
                            } else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        })
                    /*batchRemoveEmployee(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getDictionaryTypes();
                    });*/
                }).catch(() => {

                });
            },

            //批量删除明细
            batchRemoveTails: function () {
                var ids = this.sels.map(item => item.id);//[1,5]
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.patch("/dictionaryDetail", ids)
                        .then(result => {
                            this.listLoading = false;//关闭忙等框
                            //???  该完成什么事情
                            result = result.data;//AjaxResult {success:true,message:"操作成功"}
                            //AjaxResult {success:false,message:"操作失败"}
                            if (result.success) {
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                if (this.total % this.pageSize == 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getDictionaryDetails();
                            } else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        })
                    /*batchRemoveEmployee(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getDictionaryTypes();
                    });*/
                }).catch(() => {

                });
            },
            // 查询员工管理数据 用来渲染部门管理下拉菜单
            getdictionaryDetail(){
                this.$http.get("/dictionaryDetail/list",{})
                    .then(result=>{
                        result = result.data;
                        this.dictionaryDetail = result;
                    })
                    .catch(result=>{
                        alert("系统错误！")
                    })
            }/*,
			// 加载部门树，发送请求从后端拿数据
			getDeptTree(){
                this.$http.get("/dept/deptTree",{})
					.then(result=>{
					    result = result.data;
					    this.deptTree = result;
					})
					.catch(result=>{
					    alert("系统错误！");
					})
			}*/
        },
        mounted() {
            /*页面加载完成触发*/
            this.getDictionaryTypes();
            /!* 加载管理员信息*!/
            this.getdictionaryDetail();
        }
    }

</script>

<style scoped>

</style>